package com.czkt.controller;

import com.czkt.conflg.MyUserInfo;
import com.czkt.pojo.Books;
import com.czkt.pojo.Booksclass;
import com.czkt.service.BooksClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.interfaces.PBEKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
public class BooksClassController {
    @Autowired
    BooksClassService booksClassService;
    @MyUserInfo
    @RequestMapping("booksClass/list")
    @CrossOrigin
    public Object booksClassList() {
        return booksClassService.list(0);
    }

    @RequestMapping("booksClass/getSet")
    @CrossOrigin
    public Object booksClassLists(Integer classId) {
        List<Booksclass> listBooks = booksClassService.list(classId);
        Set<Long> ids = listBooks.stream().map(item -> {
            return item.getClassId();
        }).collect(Collectors.toSet());
        List<Booksclass> treeList = booksClassService.list(ids);
        Map<String, Object> setMap = new HashMap<>();
        setMap.put("treeList", treeList);
        setMap.put("listBooks", listBooks);
        return setMap;
    }
}
